class TokensController < ApplicationController
  before_filter :find_user
  # GET /users/:user_id/tokens
  # GET /users/:user_id/tokens.xml
  def index
    @tokens = @user.tokens
    @last = @user.tokens.find(:first)

    respond_to do |format|
      format.html # index.rhtml
      #format.xml  { render :xml => @tokens.to_xml }
      format.xml  { render :xml => @last.to_xml }
    end
  end

  # Only XML
  # GET /tokens/1.xml
  def show
    @token = @user.tokens.find(params[:id])
  
    respond_to do |format|
      format.xml  { render :xml => @token.to_xml }
    end
  end

  # Only HTML
  # GET /users/:user_id/tokens/new
  def new
    @token = Token.new
    @token.user = @user

    respond_to do |format|
      if @token.save!
        flash[:notice] = 'Token was successfully created.'
        format.html { redirect_to tokens_path(@user) }
      else
        format.html { redirect_to tokens_path(@user) }
      end
    end
  end

  # Only HTML
  # DELETE /users/:user_id/tokens/1
  def destroy
    @token = @user.tokens.find(params[:id])
    @token.destroy
    flash[:notice] = 'Token was successfully deleted.'

    respond_to do |format|
      format.html { redirect_to tokens_url }
    end
  end

  # Only HTML
  # GET /users/:user_id/tokens/:id;delete
  # maps to /users/:user_id/tokens/:id;destroy
  # used when JS is disabled
  def delete
    @token = @user.tokens.find(params[:id])
  end

protected
  def find_user
    @user = User.find(params[:user_id])
  end

end
